Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Originally opened as #4822 but closed because of some git branch issue.
Launch Checklist
CHANGELOG.md
under the## main
section.Description
This PR addresses how failed HTTP requests due to CORS policy are handled. So far, if a request fails because of CORS, it does not return a response, instead, it makes the browser throw a generic TypeError ( see here ). As a result, a map instance subscribing to the
"error"
event would catch a generic error that does not include the faulty URL. It is better to handle a CORS error with an AJAXError, that contains information about the URL and the cause of the actual problem.This is especially useful in the context of loading a style.json so that if a given style is not reachable because of CORS, it is possible to catch this error with the same efficiency as if fetching a style.json would yield an HTTP 5xx status code, since in both cases the request would fails due to server configuration.
This fix also addresses other network issues such as DNS resolution failures or malformed URLs
Before / After
With this minimalist snippet:
Here is the console output,
Test
I tried adding another test to
src/util/ajax.test.ts
but it is impossible to simulate a CORS with theNise
package'sFakeServer
, since a CORS error is not part of the server response but is due to the browser blocking it.Other info